package com.tju.resource.oauth.controller.sys;

import com.tju.resource.common.ServiceException;
import com.tju.resource.business.sys.SysVersionsService;
import com.tju.resource.result.Result;
import com.tju.resource.tool.tool.ChineseExceptionUtils;
import com.tju.resource.tool.tool.ClientParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

@RestController
public class SysVerController {

    public static final String prefix = "sysver";

    @Autowired
    private ChineseExceptionUtils zh_CH;

    @Autowired
    private SysVersionsService versionService;

    /**
     * 获取更新信息
     *
     * @return
     */
    @PreAuthorize("hasAuthority('sys:upd')")
    @PostMapping(prefix + "/getUpdInformation")
    public Result<Object> getUpdInformation(@RequestBody ClientParameters parameters) {
        return Optional.ofNullable(parameters)
                .filter(p -> p.getFtu() != null && p.getVersionNumber() != null)
                .map(p -> versionService.getUpdInformation(p))
                .orElseThrow(() -> new ServiceException(zh_CH.MissingParameters));
    }
    /**
     * 下载最新版本app
     *
     * @return
     */
    @RequestMapping("/download")
    public void download(HttpServletResponse response) {
         versionService.download(response);
    }
}
